package com.gx.security.message_digest;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD2Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

/**
 * Created by gx on 2017/3/28.
 */
public class ImoocMD {

    private static String src = "imooc security md";
//        jdk比较底层
//        bc提供md4
//        cc简化jdk操作
    public static void main(String[] args) {
        jdkMD5();
        System.out.println("~~~~~~~~~");
        jdkMD2();
        System.out.println("~~~~~~~~~");
        bcMD4();
        System.out.println("~~~~~~~~~");
        bcMD5();
        System.out.println("~~~~~~~~~");
        bcMD2();
        System.out.println("~~~~~~~~~");
        newBcMD4();
        System.out.println("~~~~~~~~~");
        ccMD5();
        System.out.println("~~~~~~~~~");
        ccMD2();
    }

    public static void jdkMD5() {
        try {

            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md.digest(src.getBytes());
            //将密文转成16进制
            System.out.println("jdkMD5算法实现:" + Hex.encodeHexString(md5Bytes));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static void jdkMD2() {
        try {
            MessageDigest md = MessageDigest.getInstance("MD2");
            byte[] md5Bytes = md.digest(src.getBytes());
            //将密文转成16进制
            System.out.println("jdkMD2算法实现:" + Hex.encodeHexString(md5Bytes));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static void bcMD4() {
//            Digest digest = new MD5Digest();
            Digest digest = new MD4Digest();
//            Digest digest = new MD2Digest();
//            开始加密的长度  0 ~ 最后 全部加密
            digest.update(src.getBytes(), 0, src.getBytes().length);
//            输出偏移量
            byte[] md4Bytes = new byte[digest.getDigestSize()];
            digest.doFinal(md4Bytes, 0);
            System.out.println("bc md4加密：" + org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
    }

    //jdk 中加入 bc 策略
    public static void newBcMD4() {
        try {
            //将BC 的实现加入 加密策略中
            Security.addProvider(new BouncyCastleProvider());
            MessageDigest md = MessageDigest.getInstance("MD4");
//            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md.digest(src.getBytes());
            //将密文转成16进制
            System.out.println("jdk加入 BC MD4算法实现:" + Hex.encodeHexString(md5Bytes));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    public static void bcMD5() {
            Digest digest = new MD5Digest();
//            Digest digest = new MD4Digest();
//            Digest digest = new MD2Digest();
//            开始加密的长度  0 ~ 最后 全部加密
            digest.update(src.getBytes(), 0, src.getBytes().length);
//            输出偏移量
            byte[] md4Bytes = new byte[digest.getDigestSize()];
            digest.doFinal(md4Bytes, 0);
            System.out.println("bc md5加密：" + org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
    }
    public static void bcMD2() {
//            Digest digest = new MD5Digest();
//            Digest digest = new MD4Digest();
            Digest digest = new MD2Digest();
//            开始加密的长度  0 ~ 最后 全部加密
            digest.update(src.getBytes(), 0, src.getBytes().length);
//            输出偏移量
            byte[] md4Bytes = new byte[digest.getDigestSize()];
            digest.doFinal(md4Bytes, 0);
            System.out.println("bc md2加密：" + org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
    }






//    CC实现方式  MD5
    public static void ccMD5() {
        String ccmd5 = DigestUtils.md5Hex(src.getBytes());
        System.out.println("CC MD5算法实现:"+ccmd5);
    }
//    CC实现方式  MD5
    public static void ccMD2() {
        String ccmd2 = DigestUtils.md2Hex(src.getBytes());
        System.out.println("CC MD2算法实现:"+ccmd2);
    }
}
